import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.imageio.ImageIO;

import net.semanticmetadata.lire.imageanalysis.ImagePoint;
import net.semanticmetadata.lire.imageanalysis.MSER;
import net.semanticmetadata.lire.imageanalysis.MSERGrowthHistory;


public class MyImage {
	
	private String name;
	private String location;
	private String type;
	
	private BufferedImage bImage; 
	double[] tfIdf;
	               
	HashMap<String, MyRegion> regionList = null;
	//new ArrayList<Region>()
	
	
	
/*
	public MyImage(String name) {
		super();
		this.name = name;
		regionList = new ArrayList<Region>();
		try { bImage = ImageIO.read(new File("G:\\omer\\"+name+".jpg")); } catch (IOException e) { }
		CreateRegions();
	}
*/


	/**
	 * 
	 */
	public MyImage(String name, String location) {
		super();
		
		String[] st;
		st = name.split("\\.");
		
		this.name = st[0];
		this.location = location;
		this.type = st[1];
		bImage = null;
		regionList = new HashMap<String,MyRegion>();
		
		

		/*In the constructor*/
		try { bImage = ImageIO.read(new File(location)); } catch (IOException e) { }
		CreateRegions();
		
		
	}
	


	public MyImage() {
	}






	/**
	 * @param bImage
	 */
	public MyImage(BufferedImage bImage) {
		super();
		this.bImage = bImage;
		this.name = "queryImage";
		regionList = new HashMap<String,MyRegion>();
		
		CreateRegions();
	}



	public void CreateRegions(){
		MSERGrowthHistory[] initRegions ;
		
		MSER mser = new MSER();	
		initRegions = mser.extractMSER(bImage);
	
		for (int j = 0; j < initRegions.length;j++)
		{
			ImagePoint[] list = initRegions[j].getPoints();
	
			int minX = bImage.getWidth();
			int minY = bImage.getHeight();
			int maxX = 0;
			int maxY = 0;
			for (int i = 0 ; i < list.length ; i++)
			{
				if (minX > list[i].getX())
					minX =  list[i].getX();
				if (maxX <  list[i].getX())
					maxX =  list[i].getX();
				if (minY >  list[i].getY())
					minY =  list[i].getY();
				if (maxY <  list[i].getY())
					maxY =  list[i].getY();
			}
			int w = maxX - minX+1;
			int h = maxY - minY+1;
			BufferedImage temp = new BufferedImage(bImage.getWidth(),bImage.getHeight(),BufferedImage.TYPE_INT_BGR);
			
	
			for (int i = 0; i<bImage.getWidth();i++)
				for (int k = 0; k<bImage.getHeight();k++)
					temp.setRGB(i,k,255);
	
			for (int i=0; i< list.length; i++)
			{
				int x = list[i].getX();
				int y = list[i].getY();
				int c = bImage.getRGB(x,y);
				temp.setRGB(x,y,c);
			}
			BufferedImage regionBufferedImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_BGR);
			regionBufferedImage = temp.getSubimage(minX, minY, w, h);
			BufferedImage after = new BufferedImage(w*5,h*5, BufferedImage.TYPE_INT_BGR);
			AffineTransform at = new AffineTransform();
			at.scale(5.0, 5.0);
			AffineTransformOp scaleOp = 
			   new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
			after = scaleOp.filter(regionBufferedImage, after);
			MyRegion region = new MyRegion(minX,minY,after,h*10,w*10,name+"-"+j,name);
			regionList.put(region.getId(),region);  
			
			/*File outputfile = new File("C:\\omer\\"+name+j+".png");
	        try {
				ImageIO.write(after, "png", outputfile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			*/
		}

	
	
	}
	
	/**
	 * @return the regionList
	 */
	public HashMap<String,MyRegion> getRegionList() {
		return regionList;
	}



	/**
	 * @param regionList the regionList to set
	 */
	public void setRegionList(HashMap<String,MyRegion> regionList) {
		this.regionList = regionList;
	}



	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}



	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}



	/**
	 * @return the location
	 */
	public String getLocation() {
		return location;
	}



	/**
	 * @param location the location to set
	 */
	public void setLocation(String location) {
		this.location = location;
	}



	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}



	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}



	/**
	 * @return the bImage
	 */
	public BufferedImage getbImage() {
		return bImage;
	}



	/**
	 * @param bImage the bImage to set
	 */
	public void setbImage(BufferedImage bImage) {
		this.bImage = bImage;
	}

	

}
